package com.telpoo.bhlib.share.facebook;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.view.KeyEvent;

import com.facebook.FacebookException;
import com.facebook.FacebookOperationCanceledException;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.widget.WebDialog;
import com.facebook.widget.WebDialog.FeedDialogBuilder;
import com.facebook.widget.WebDialog.OnCompleteListener;
import com.telpoo.frame.delegate.Idelegate;

public class ManagerFacebook {
	private Activity mActivity;
	final List<String> PERMISSIONS = Arrays.asList("status_update");

	public ManagerFacebook(Activity mActivity) {
		this.mActivity = mActivity;
	}

	public void updateStatus(final String name, final String caption, final String description, final String link, final String urlPicture, final Activity activity,
			final Idelegate listenerShareFacebook) {
		mActivity = activity;

		// start Facebook Login
		Session.openActiveSession(activity, true, new Session.StatusCallback() {
			// callback when session changes state
			public void call(final Session session, SessionState state, Exception exception) {
				if (session.isOpened()) {
					publishFeedDialog(name, caption, description, link, urlPicture, listenerShareFacebook);

				}

			}
		});
	}

	public boolean checkPermissions() {
		Session s = Session.getActiveSession();
		if (s != null) {
			return s.getPermissions().contains("publish_actions");
		} else
			return false;
	}

	public void requestPermissions() {
		Session s = Session.getActiveSession();
		if (s != null)
			s.requestNewPublishPermissions(new Session.NewPermissionsRequest(mActivity, PERMISSIONS));
	}

	private void publishFeedDialog(String name, String caption, String description, String link, String urlPicture, final Idelegate listenerShareFacebook) {
		Bundle params = new Bundle();

		params.putString("name", name);
		params.putString("description", description);
		params.putString("link", link);
		params.putString("picture", urlPicture);
		params.putString("method", "photos.upload");
		Session session = Session.getActiveSession();

		FeedDialogBuilder feedDialogbuilder = new WebDialog.FeedDialogBuilder(mActivity, session, params);
		WebDialog feedDialog = feedDialogbuilder.build();
		feedDialog.setOnCompleteListener(new OnCompleteListener() {

			public void onComplete(Bundle values, FacebookException error) {
				if (error == null) {
					// When the story is posted, echo the success
					// and the post Id.
					final String postId = values.getString("post_id");
					if (postId != null) {
						listenerShareFacebook.callBack("Posted!", 0);
					} else {
						// User clicked the Cancel button
						listenerShareFacebook.callBack("Publish cancelled", 1);
					}
				} else if (error instanceof FacebookOperationCanceledException) {
					// User clicked the "x" button
					listenerShareFacebook.callBack("Publish cancelled", 2);
				} else {
					// Generic, ex: network error
					listenerShareFacebook.callBack("Error posting story", 3);
				}

				mActivity.finish();
			}

		});

		feedDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				// Toast.makeText(mActivity, "1111", 1).show();
				mActivity.finish();

			}
		});

		feedDialog.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
				// Toast.makeText(mActivity, "1111-" + arg2, 1).show();
				return false;
			}
		});

		feedDialog.show();
	}

}